local ma__lianhua = fk.CreateSkill {
  name = "ma__lianhua",
}

Fk:loadTranslationTable{
  ["ma__lianhua"] = "炼化",
  [":ma__lianhua"] = "一名角色的出牌阶段开始时，若其手牌数不小于你，你可以观看其手牌并弃置其中至少一张花色不同的牌，"..
  "然后令其从游戏外获得X张随机生成的牌（进入弃牌堆后销毁，X为弃置的牌数+1）。",

  ["#ma__lianhua-recast"] = "炼化：选择 %dest 至少一张花色不同的手牌，弃置这些牌并令其获得等量+1张随机牌",
  ["@@ma__lianhua__destr_discard"] = "炼化",

  ["$ma__lianhua1"] = "抱朴子之论，吾必专研。",
  ["$ma__lianhua2"] = "得太清丹经指点，可知一二。",
}

Fk:addPoxiMethod{
  name = "ma__lianhua",
  prompt = function (data, extra_data)
    return "#ma__lianhua-recast::"..extra_data.id
  end,
  card_filter = function(to_select, selected, data)
    local suit = Fk:getCardById(to_select).suit
    if suit == Card.NoSuit then return false end
    return not table.find(selected, function(id) return Fk:getCardById(id).suit == suit end)
    and not (Self:prohibitDiscard(Fk:getCardById(to_select)) and table.contains(data[1][2], to_select))
  end,
  feasible = function(selected)
    return #selected ~= 0
  end,
  default_choice = function (data, extra_data)
    return {data[1]}
  end,
}

ma__lianhua:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(ma__lianhua.name) and target.phase == Player.Play and
      not target.dead and target:getHandcardNum() >= player:getHandcardNum()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not target:isKongcheng() then
      local cards
      if target == player then
        local success, dat = room:askToUseActiveSkill(player, {
          skill_name = "ma__lianhua_active",
          prompt = "#ma__lianhua-recast::"..target.id,
          cancelable = false,
        })
        if not (success and dat) then
          dat = {}
          local card = table.filter(player:getCardIds("h"), function (id)
            return not player:prohibitDiscard(id) and Fk:getCardById(id).suit ~= Card.NoSuit
          end)
          dat.cards = {card[1]}
        end
        cards = dat.cards
      else
        --[[cards = room:askToChooseCards(player, {
          target = target,
          min = 1,
          max = 999,
          flag = { card_data = {{ target.general, target:getCardIds("h") }} },
        skill_name = ma__lianhua.name,
          prompt = "#ma__lianhua-recast::"..target.id,
        })]]
        local extra_data = {}
        extra_data.id = target.id
        cards = room:askToPoxi(player, {
          poxi_type = ma__lianhua.name,
          data = { { target.general, target:getCardIds("h") } },
          cancelable = true,
          extra_data = extra_data,
        })
      end
      if #cards > 0 then
        local n = #cards +1
        room:throwCard(cards, ma__lianhua.name, target, player)
        local ids = {}
        for i = 1, n do
          local card_name = table.random(Fk:getAllCardNames("btde"), 1)[1]
          local card_suit = table.random({Card.Spade, Card.Heart, Card.Club, Card.Diamond}, 1)[1]
          local card = room:printCard(card_name, card_suit, math.random(1,13))
          room:setCardMark(card, MarkEnum.DestructIntoDiscard, 1)
          room:setCardMark(card, "@@ma__lianhua__destr_discard", 1)
          table.insert(ids, card.id)
        end
          room:moveCards({
          ids = ids,
          to = target,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skill_name = ma__lianhua.name,
          moveVisible = false,
        })
      end
    end
  end,
})

return ma__lianhua